package com.woniu113.junk.gateway.filter;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

/**
 * 自定义过滤器工厂
 *
 * 泛型为参数类型
 */
@Component
public class TestGatewayFilterFactory extends AbstractGatewayFilterFactory<TestGatewayFilterFactory.TestConfig> {

    /**
     * TestGatewayFilterFactory的无参构造方法
     */
    public TestGatewayFilterFactory(){
        super(TestConfig.class);
    }

    /**
     * 手动重写规定参数顺序的方法
     */
    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("value","name","key");
    }

    /**
     *重写工厂方法
     */
    @Override
    public GatewayFilter apply(TestConfig config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                System.out.println(config.name+":"+config.getKey()+":"+config.getValue());
                return chain.filter(exchange);//通过
            }
        };
    }

    /**
     * 测试参数
     */
    @Data
    static class TestConfig{
        private String name;
        private String key;
        private String value;
    }
}
